<?php
namespace App\Http\Controllers\desktop\publics;

use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use App\Models\Member;

class LoginController extends BaseController {
	
	public function index() {
		if(Auth::check()) {
			return redirect('/cs');
		}
		return view('desktop.publics.login');
	}
	
	public function attempt() {
		if(Auth::check()) {
			return redirect('/cs');
		}
		$input = Input::all();
		$rules = [
			'cellphone' => 'required',
			'password' => 'required',
		];
		$validation = validator($input, $rules, Member::$msgs);
		if ($validation->passes()) {
			if(Auth::attempt([
				'cellphone' => $input['cellphone'],
				'password' => $input['password']
			], true)) {
				$member = Auth::user();
				if(!is_null($member)) {
					return redirect('/');
				}
			}
			Session::flash('msg', '您输入的帐号或密码有误。');
		}
		return Redirect::back()
				->withInput()
				->withErrors($validation);
	}

	public function logout() {
		Auth::logout();
		return Redirect::back();
	}
}
